package org.mountain.music.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

import org.mountain.music.R;
import org.mountain.music.fragment.AFragment;
import org.mountain.music.fragment.BFragment;
import org.mountain.music.fragment.CFragment;
import org.mountain.music.fragment.DFragment;
import org.mountain.music.service.MusicService;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements AFragment.OnFragmentInteractionListener, BFragment.OnFragmentInteractionListener, CFragment.OnFragmentInteractionListener, DFragment.OnFragmentInteractionListener, View.OnClickListener {
    private List<Fragment> fragmentlist;
    private Fragment aFragment;
    private Fragment bFragment;
    private Fragment cFragment;
    private Fragment dFragment;
    private ViewPager viewPager;
    private Button album_button;
    private Button list_button;
    private Button word_button;
    private Button setting_button;
    private ImageButton lastone_button;
    private ImageButton nextont_button;
    private ImageButton start_button;
    private Handler handler;
    public static BFragment.FragmentonMessage fragmentonMessage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }


    public static boolean isGrantExternalRW(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && activity.checkSelfPermission(
                Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

            activity.requestPermissions(new String[]{
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
            }, 1);

            return false;
        }

        return true;
    }

    private void initView() {

        isGrantExternalRW(this);
        fragmentonMessage = new BFragment.FragmentonMessage() {
            @Override
            public void handleMessage(String msg) {

                Message message = new Message();
                message.what = 1;
                message.obj = msg;
                handler.sendMessage(message);
            }
        };
        viewPager =  findViewById(R.id.viewPager);
        album_button = findViewById(R.id.album_button);
        list_button =  findViewById(R.id.list_button);
        word_button =  findViewById(R.id.word_button);
        setting_button =  findViewById(R.id.setting_button);
        lastone_button =  findViewById(R.id.lastone_button);
        nextont_button =  findViewById(R.id.nextone_button);
        start_button = findViewById(R.id.start_stop_button);

        lastone_button.setOnClickListener(this);
        nextont_button.setOnClickListener(this);
        start_button.setOnClickListener(this);
        album_button.setOnClickListener(this);
        list_button.setOnClickListener(this);
        word_button.setOnClickListener(this);
        setting_button.setOnClickListener(this);
    }

    private void changetab(int position) {

        switch (position) {
            case R.id.album_button:
                viewPager.setCurrentItem(0);
            case 0:
                album_button.setSelected(true);
                list_button.setSelected(false);
                word_button.setSelected(false);
                setting_button.setSelected(false);

                break;

            case R.id.list_button:
                viewPager.setCurrentItem(1);
            case 1:
                album_button.setSelected(false);
                list_button.setSelected(true);
                word_button.setSelected(false);
                setting_button.setSelected(false);

                break;
            case R.id.word_button:
                viewPager.setCurrentItem(2);

            case 2:
                album_button.setSelected(false);
                list_button.setSelected(false);
                word_button.setSelected(true);
                setting_button.setSelected(false);

                break;
            case R.id.setting_button:
                viewPager.setCurrentItem(3);
            case 3:
                album_button.setSelected(false);
                list_button.setSelected(false);
                word_button.setSelected(false);
                setting_button.setSelected(true);

                break;

            case R.id.lastone_button:
                break;
            case R.id.nextone_button:


                break;
            case R.id.start_stop_button:


                break;


        }
    }

    private void startMusicService(String option, String path) {
        Intent intentService = new Intent(MainActivity.this, MusicService.class);
        intentService.putExtra("option", option);
        intentService.putExtra("messenger", new Messenger(handler));
        intentService.putExtra("path", path);
        startService(intentService);
    }

    private void startMusicService(String option) {
        Intent intentService = new Intent(MainActivity.this, MusicService.class);
        intentService.putExtra("option", option);
        intentService.putExtra("messenger", new Messenger(handler));
        startService(intentService);
    }

    private void startMusicService(String option, int progress) {
        Intent intentService = new Intent(MainActivity.this, MusicService.class);
        intentService.putExtra("option", option);
        intentService.putExtra("progress", progress);
        intentService.putExtra("messenger", new Messenger(handler));
        startService(intentService);
    }

    private void initData() {

        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case 1:
                        start_button.setImageResource(R.drawable.stop);
                        break;

                }

            }
        };
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                changetab(position);
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        viewPager.setOffscreenPageLimit(4);
        fragmentlist = new ArrayList<>();
        aFragment = new AFragment();
        bFragment = new BFragment();
        cFragment = new CFragment();
        dFragment = new DFragment();
        fragmentlist.add(aFragment);
        fragmentlist.add(bFragment);
        fragmentlist.add(cFragment);
        fragmentlist.add(dFragment);
        FragmentPagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentlist.get(position);
            }

            @Override
            public int getCount() {
                return fragmentlist.size();
            }
        };
        viewPager.setAdapter(adapter);

    }

    @Override
    public void onFragmentInteraction(Uri uri) {


    }

    @Override
    public void onClick(View v) {

        changetab(v.getId());


    }


}
